package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.cmd.SchedulerCmd;

import com.ymk.bo.member.MemberBO;
import com.ymk.bo.stat.StatMchDayBO;
import com.ymk.core.code.cmd.MemberCmdCode;
import com.ymk.core.code.cmd.OrdersCmdCode;
import com.ymk.core.code.cmd.StatMchCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.service.IStatMchDayService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class StatMchDayPostCmd extends ACmd {

    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        StatMchDayBO reqData = super.getData(sign, StatMchDayBO.class);

        StatMchDayBO statMchDayBO = new StatMchDayBO();
        statMchDayBO.setMchId(reqData.getMchId());
        statMchDayBO.setStatDate(reqData.getStatDate());

        // 1.获取商户新增会员数量
        CmdSign memberCmd = CmdSign.Builder.newBuild(sign)
                .setCmdCode(MemberCmdCode.MEMBER_LIST)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .setSource(statMchDayBO)
                .build();

        List<MemberBO> memberBOLis = this.scheduler.schedule(request, memberCmd, MemberBO.class, true);
        Integer memberCount = memberBOLis.size();


        statMchDayBO.setMemberQuantity(memberCount);
        // 2.获取商户新增订单数量
        CmdSign orderrCmd = CmdSign.Builder.newBuild(sign)
                .setCmdCode(OrdersCmdCode.ORDERS_LIST)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .setSource(statMchDayBO)
                .build();
        List<Orders> orders = this.scheduler.schedule(request, orderrCmd, Orders.class, true);

        statMchDayBO.setOrdersQuantity(orders.size());


        // 3.获取商户的销量
        Integer goodsCount = orders.stream().collect(Collectors.summingInt(Orders::getGoodsCount));
        statMchDayBO.setSales(BigDecimal.valueOf(goodsCount));

        // 4.获取商户销售金额
        Map<Integer, BigDecimal> ordersGroup = orders.stream().collect(Collectors.groupingBy(Orders::getId
                , Collectors.mapping(Orders::getOrdersAmount, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
        BigDecimal payQuantity = new BigDecimal(0);
        for (Integer i : ordersGroup.keySet()){
            payQuantity =  payQuantity.add(ordersGroup.get(i));
        }
        statMchDayBO.setPayQuantity(payQuantity);
        this.setCreatedData(sign,statMchDayBO);

        this.statMchDayService.post(statMchDayBO);

        return respSign;
    }


    @Override
    public String getCmdCode() {
        return StatMchCmdCode.STAT_MCH_DAY_POST;
    }

    @Autowired
    private IStatMchDayService statMchDayService;

    @Autowired
    protected SchedulerCmd scheduler;



}